<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/3-14:59
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller;


    use app\admin\service\CategoryService;
    use app\admin\validate\ArticleValidate;
    use Jrk\Excel;
    use Jrk\Strings;
    use Jrk\Trees;

    class Article extends AdminBase
    {


        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            $this->model = app()->model("Articles");

            $this->del = "文章";

            $this->assign("tree", Trees::toFormatTree(CategoryService::selcectAll(), 'name'));

        }


        /**
         * @return mixed
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12 0012
         * @name: addArticle
         * @describe:文章添加编辑
         */
        public function addArticle()
        {
            if (IS_POST) {
                $data     = $this->request->post();
                $validate = new ArticleValidate();

                if (!$validate->check($data)) {
                    $this->error($validate->getError());
                }

                $data['content'] = htmlspecialchars($data['content']);

                if (empty($data['description'])) {
                    $data['description'] = Strings::str_cut($data['content'], 200);
                }

                if (isset($data['id'])) {
                    addLog("文章修改操作", "修改文章：".$data['title']);
                    $res = $this->model->addAndEdit($data);
                } else {
                    addLog("文章添加操作", "新增文章：".$data['title']);
                    $res = $this->model->add($data);
                }

                return $res;
            }

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/4
         * @name: editArticle
         * @describe:编辑
         */
        public function editArticle(){

            $id   = $this->request->param("id/d");//父id

            $info = $this->model->getOne(['id'=>$id]);

            if (empty($info)){
                $this->assign('msg','当前id:'.$id.'错误');
                $this->fetch("public/404");
            }

            $this->assign(compact( "info"));


            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/5
         * @name: removeCategory
         * @describe:移动
         */
        public function removeCategory(){
            if (IS_POST){
                $param=$this->request->param();
                return $this->model->removeCategory($param['ids'],$param['category_id']);
            }
        }


        /**
         * @throws \PHPExcel_Reader_Exception
         * @throws \PHPExcel_Writer_Exception
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/17 0017
         * @name: export
         * @describe:文章导出
         */
        public function export(){
            $ids=$this->request->param("id");
            if ($ids=="all"){
                $list=$this->model->order("create_time desc")->select();
            }else{
                $list=$this->model->where('id',"in",$ids)->order("create_time desc")->select();
            }
            if (empty($list)){
                $this->redirect(url("article/index"));exit;
            }
            $arr=[];
            foreach ($list as $v){
                $c=CategoryService::get($v['category_id'])['name'];
               $arr[]=[
                   $v['title'],$c,$v['keywords'],$v['description'],$this->domain."/".$v['image_url'],
                   $this->domain."/".$v['url'],$v['author'],$v['source'],$v['hits'],$v['comment_num']
               ];
            }
            Excel::setExcelHeader(['文章标题','分类','关键词','文章描述','首图地址','链接地址','作者','来源','点击量','评论量'])
                ->setExcelTile('文章列表数据', '文章数据',date('Y-m-d H:i:s',time()))
                ->setExcelContent($arr)
                ->ExcelSave();
        }

    }